{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Client\n",
    "\n",
    "Demo of a client interacting with a remote agent. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can interact with this via API directly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'output': {'output': 'Eugene thinks that cats like fish.'},\n",
       " 'callback_events': [],\n",
       " 'metadata': {'run_id': 'f16d95e5-dd8f-48d1-8668-4b33a54023fb'}}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "inputs = {\"input\": {\"input\": \"what does eugene think of cats?\"}}\n",
    "response = requests.post(\"http://localhost:8000/invoke\", json=inputs)\n",
    "\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also interact with this via the RemoteRunnable interface (to use in other chains)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langserve import RemoteRunnable\n",
    "\n",
    "remote_runnable = RemoteRunnable(\"http://localhost:8000/\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remote runnable has the same interface as local runnables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'output': 'Hello! How can I assist you today?'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "await remote_runnable.ainvoke({\"input\": \"hi!\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'output': 'Eugene thinks that cats like fish.'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "remote_runnable.invoke({\"input\": \"what does eugene think of cats?\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stream\n",
    "\n",
    "Please note that streaming alternates between actions and observations. It does not stream individual tokens! If you need to stream individual tokens you will need to use astream_log!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--\n",
      "{'actions': [AgentActionMessageLog(tool='get_eugene_thoughts', tool_input={'query': 'cats'}, log=\"\\nInvoking: `get_eugene_thoughts` with `{'query': 'cats'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})])], 'messages': [AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})]}\n",
      "--\n",
      "{'steps': [{'action': AgentActionMessageLog(tool='get_eugene_thoughts', tool_input={'query': 'cats'}, log=\"\\nInvoking: `get_eugene_thoughts` with `{'query': 'cats'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})]), 'observation': [Document(page_content='cats like fish'), Document(page_content='dogs like sticks')]}], 'messages': [FunctionMessage(content=\"[Document(page_content='cats like fish'), Document(page_content='dogs like sticks')]\", name='get_eugene_thoughts')]}\n",
      "--\n",
      "{'output': \"Eugene thinks that cats like fish. Now let me tell you a story about that thought.\\n\\nOnce upon a time, in a small village, there lived a mischievous cat named Whiskers. Whiskers was known for his love for fish. Every day, he would venture out to the nearby river in search of his favorite food.\\n\\nOne sunny day, Whiskers set out on his usual fish-hunting expedition. As he approached the riverbank, he noticed a group of fishermen casting their nets into the water. Whiskers couldn't resist the temptation and decided to join in on the action.\\n\\nWith his agile paws, Whiskers skillfully maneuvered through the fishermen, strategically positioning himself to snatch the fish caught in their nets. The fishermen were amazed by Whiskers' quick reflexes and couldn't help but laugh at the sight of a cat fishing alongside them.\\n\\nWhiskers continued his fish-stealing escapades for several days, becoming somewhat of a local legend. People from neighboring villages would come to witness the incredible sight of a cat outsmarting seasoned fishermen.\\n\\nOne day, as Whiskers was enjoying his stolen fish by the river, he noticed a stray dog named Rover approaching him. Rover had heard about Whiskers' fishing talents and was intrigued by the cat's abilities.\\n\\nCurious, Whiskers decided to share his secret with Rover. He taught him the art of stealth and how to snatch fish from the nets without being noticed. Rover, being a quick learner, soon became Whiskers' partner in crime.\\n\\nTogether, Whiskers and Rover formed an unbeatable duo, leaving the fishermen puzzled and amazed at the disappearing fish. The villagers, entertained by their antics, started leaving fish out for Whiskers and Rover as a token of appreciation.\\n\\nAnd so, Whiskers and Rover continued their fish-stealing adventures, bringing joy and laughter to the village. Eugene's thought about cats and their love for fish certainly came to life in the mischievous and clever Whiskers, who proved that cats truly have a special affinity for fish.\", 'messages': [AIMessage(content=\"Eugene thinks that cats like fish. Now let me tell you a story about that thought.\\n\\nOnce upon a time, in a small village, there lived a mischievous cat named Whiskers. Whiskers was known for his love for fish. Every day, he would venture out to the nearby river in search of his favorite food.\\n\\nOne sunny day, Whiskers set out on his usual fish-hunting expedition. As he approached the riverbank, he noticed a group of fishermen casting their nets into the water. Whiskers couldn't resist the temptation and decided to join in on the action.\\n\\nWith his agile paws, Whiskers skillfully maneuvered through the fishermen, strategically positioning himself to snatch the fish caught in their nets. The fishermen were amazed by Whiskers' quick reflexes and couldn't help but laugh at the sight of a cat fishing alongside them.\\n\\nWhiskers continued his fish-stealing escapades for several days, becoming somewhat of a local legend. People from neighboring villages would come to witness the incredible sight of a cat outsmarting seasoned fishermen.\\n\\nOne day, as Whiskers was enjoying his stolen fish by the river, he noticed a stray dog named Rover approaching him. Rover had heard about Whiskers' fishing talents and was intrigued by the cat's abilities.\\n\\nCurious, Whiskers decided to share his secret with Rover. He taught him the art of stealth and how to snatch fish from the nets without being noticed. Rover, being a quick learner, soon became Whiskers' partner in crime.\\n\\nTogether, Whiskers and Rover formed an unbeatable duo, leaving the fishermen puzzled and amazed at the disappearing fish. The villagers, entertained by their antics, started leaving fish out for Whiskers and Rover as a token of appreciation.\\n\\nAnd so, Whiskers and Rover continued their fish-stealing adventures, bringing joy and laughter to the village. Eugene's thought about cats and their love for fish certainly came to life in the mischievous and clever Whiskers, who proved that cats truly have a special affinity for fish.\")]}\n"
     ]
    }
   ],
   "source": [
    "async for chunk in remote_runnable.astream({\"input\": \"what does eugene think of cats? Then tell me a story about that thought.\"}):\n",
    "    print('--')\n",
    "    print(chunk)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stream Events\n",
    "\n",
    "The client is looking for a runnable name called `agent` for the chain events. This name was defined on the server side using `runnable.with_config({\"run_name\": \"agent\"}`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting agent: agent with input: {'input': 'what does eugene think of cats? Then tell me a story about that thought.'}\n",
      "--\n",
      "Starting tool: get_eugene_thoughts with inputs: {'query': 'cats'}\n",
      "Done tool: get_eugene_thoughts\n",
      "Tool output was: [Document(page_content='cats like fish'), Document(page_content='dogs like sticks')]\n",
      "--\n",
      "E|ug|ene| thinks| that| cats| like| fish|.| Now| let| me| tell| you| a| story| about| that| thought|:\n",
      "\n",
      "|Once| upon| a| time|,| in| a| small| village|,| there| lived| a| curious| cat| named| Wh|isk|ers|.| Wh|isk|ers| was| known| for| his| love| of| fish|.| Every| day|,| he| would| venture| to| the| nearby| river| in| search| of| his| favorite| meal|.\n",
      "\n",
      "|One| sunny| morning|,| Wh|isk|ers| set| out| on| his| daily| fish|-h|unting| expedition|.| As| he| approached| the| river|,| he| could| smell| the| fresh| scent| of| the| water| and| feel| the| excitement| building| up| inside| him|.| Wh|isk|ers| knew| that| today| might| be| his| lucky| day|.\n",
      "\n",
      "|He| carefully| ti|pto|ed| along| the| river|bank|,| his| eyes| fixed| on| the| water|'s| surface|.| Suddenly|,| he| spotted| a| shimmer|ing| fish| swimming| gracefully| through| the| clear| blue| water|.| Wh|isk|ers| c|rou|ched| low|,| ready| to| p|ounce|.\n",
      "\n",
      "|With| lightning| speed|,| he| le|aped| into| the| air|,| his| p|aws| out|st|retched| towards| the| fish|.| Splash|!| Wh|isk|ers| landed| right| in| the| middle| of| the| river|,| causing| r|ipples| to| spread| in| all| directions|.| But| he| didn|'t| care|.| All| he| wanted| was| that| delicious| fish|.\n",
      "\n",
      "|Wh|isk|ers| chased| the| fish| with| all| his| might|,| dart|ing| through| the| water| with| elegance| and| precision|.| The| fish| sw|am| gracefully|,| trying| to| escape| Wh|isk|ers|'| determined| pursuit|.| But| the| cat| was| relentless|.\n",
      "\n",
      "|After| a| few| moments| of| intense| chase|,| Wh|isk|ers| finally| managed| to| catch| the| fish| in| his| p|aws|.| He| triumph|antly| carried| it| to| the| river|bank|,| where| he| enjoyed| his| well|-des|erved| feast|.| The| taste| of| the| fresh| fish| was| heavenly|,| satisfying| his| hunger| and| bringing| a| content|ed| smile| to| his| face|.\n",
      "\n",
      "|From| that| day| on|,| Wh|isk|ers| became| known| as| the| legendary| fish|-catching| cat| in| the| village|.| People| would| often| gather| by| the| river| to| watch| him| in| action|,| amazed| by| his| agility| and| determination|.| Wh|isk|ers| taught| everyone| the| importance| of| perseverance| and| following| one|'s| passion|,| just| like| he| pursued| his| love| for| fish|.\n",
      "\n",
      "|And| so|,| Wh|isk|ers| continued| his| fish|-h|unting| adventures|,| spreading| joy| and| inspiration| to| everyone| he| encountered|.| He| proved| that| when| you| have| a| passion| for| something|,| nothing| can| stop| you| from| achieving| it| –| just| like| cats| and| their| love| for| fish|.\n",
      "\n",
      "|The| end|.|\n",
      "--\n",
      "Done agent: agent with output: Eugene thinks that cats like fish. Now let me tell you a story about that thought:\n",
      "\n",
      "Once upon a time, in a small village, there lived a curious cat named Whiskers. Whiskers was known for his love of fish. Every day, he would venture to the nearby river in search of his favorite meal.\n",
      "\n",
      "One sunny morning, Whiskers set out on his daily fish-hunting expedition. As he approached the river, he could smell the fresh scent of the water and feel the excitement building up inside him. Whiskers knew that today might be his lucky day.\n",
      "\n",
      "He carefully tiptoed along the riverbank, his eyes fixed on the water's surface. Suddenly, he spotted a shimmering fish swimming gracefully through the clear blue water. Whiskers crouched low, ready to pounce.\n",
      "\n",
      "With lightning speed, he leaped into the air, his paws outstretched towards the fish. Splash! Whiskers landed right in the middle of the river, causing ripples to spread in all directions. But he didn't care. All he wanted was that delicious fish.\n",
      "\n",
      "Whiskers chased the fish with all his might, darting through the water with elegance and precision. The fish swam gracefully, trying to escape Whiskers' determined pursuit. But the cat was relentless.\n",
      "\n",
      "After a few moments of intense chase, Whiskers finally managed to catch the fish in his paws. He triumphantly carried it to the riverbank, where he enjoyed his well-deserved feast. The taste of the fresh fish was heavenly, satisfying his hunger and bringing a contented smile to his face.\n",
      "\n",
      "From that day on, Whiskers became known as the legendary fish-catching cat in the village. People would often gather by the river to watch him in action, amazed by his agility and determination. Whiskers taught everyone the importance of perseverance and following one's passion, just like he pursued his love for fish.\n",
      "\n",
      "And so, Whiskers continued his fish-hunting adventures, spreading joy and inspiration to everyone he encountered. He proved that when you have a passion for something, nothing can stop you from achieving it – just like cats and their love for fish.\n",
      "\n",
      "The end.\n"
     ]
    }
   ],
   "source": [
    "async for event in remote_runnable.astream_events(\n",
    "    {\"input\": \"what does eugene think of cats? Then tell me a story about that thought.\"},\n",
    "    version=\"v1\",\n",
    "):\n",
    "    kind = event[\"event\"]\n",
    "    if kind == \"on_chain_start\":\n",
    "        if (\n",
    "            event[\"name\"] == \"agent\"\n",
    "        ):  # Was assigned when creating the agent with `.with_config({\"run_name\": \"Agent\"})`\n",
    "            print(\n",
    "                f\"Starting agent: {event['name']} with input: {event['data'].get('input')}\"\n",
    "            )\n",
    "    elif kind == \"on_chain_end\":\n",
    "        if (\n",
    "            event[\"name\"] == \"agent\"\n",
    "        ):  # Was assigned when creating the agent with `.with_config({\"run_name\": \"Agent\"})`\n",
    "            print()\n",
    "            print(\"--\")\n",
    "            print(\n",
    "                f\"Done agent: {event['name']} with output: {event['data'].get('output')['output']}\"\n",
    "            )\n",
    "    if kind == \"on_chat_model_stream\":\n",
    "        content = event[\"data\"][\"chunk\"].content\n",
    "        if content:\n",
    "            # Empty content in the context of OpenAI means\n",
    "            # that the model is asking for a tool to be invoked.\n",
    "            # So we only print non-empty content\n",
    "            print(content, end=\"|\")\n",
    "    elif kind == \"on_tool_start\":\n",
    "        print(\"--\")\n",
    "        print(\n",
    "            f\"Starting tool: {event['name']} with inputs: {event['data'].get('input')}\"\n",
    "        )\n",
    "    elif kind == \"on_tool_end\":\n",
    "        print(f\"Done tool: {event['name']}\")\n",
    "        print(f\"Tool output was: {event['data'].get('output')}\")\n",
    "        print(\"--\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stream log\n",
    "\n",
    "If you need acccess the individual llm tokens from an agent use `astream_log`. Please make sure that you set **streaming=True** on your LLM (see server code). For this to work, the LLM must also support streaming!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--\n",
      "RunLogPatch({'op': 'replace',\n",
      "  'path': '',\n",
      "  'value': {'final_output': None,\n",
      "            'id': '1213bf75-c5ff-401a-b3c5-b4663ec7dfca',\n",
      "            'logs': {},\n",
      "            'streamed_output': []}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableSequence',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '7629fc73-a2fd-48fa-b9a2-c336e270180d',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'RunnableSequence',\n",
      "            'start_time': '2024-01-05T22:24:50.737+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': [],\n",
      "            'type': 'chain'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableParallel<input,agent_scratchpad>',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '72d2e143-d977-4661-b901-430cae1ee739',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'RunnableParallel<input,agent_scratchpad>',\n",
      "            'start_time': '2024-01-05T22:24:50.738+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:1'],\n",
      "            'type': 'chain'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '7ec78a86-596f-4904-9888-45ef67302cb8',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': '<lambda>',\n",
      "            'start_time': '2024-01-05T22:24:50.739+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['map:key:input'],\n",
      "            'type': 'chain'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '281ca194-3e0c-490c-b637-759f5d971769',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': '<lambda>',\n",
      "            'start_time': '2024-01-05T22:24:50.739+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['map:key:agent_scratchpad'],\n",
      "            'type': 'chain'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>/final_output',\n",
      "  'value': {'output': 'what does eugene think of cats?'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/<lambda>/end_time',\n",
      "  'value': '2024-01-05T22:24:50.740+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>:2/final_output',\n",
      "  'value': {'output': []}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/<lambda>:2/end_time',\n",
      "  'value': '2024-01-05T22:24:50.740+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableParallel<input,agent_scratchpad>/final_output',\n",
      "  'value': {'agent_scratchpad': [],\n",
      "            'input': 'what does eugene think of cats?'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/RunnableParallel<input,agent_scratchpad>/end_time',\n",
      "  'value': '2024-01-05T22:24:50.740+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatPromptTemplate',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '68c6ad9d-b9f0-47e0-9ea7-d0f170a22732',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'ChatPromptTemplate',\n",
      "            'start_time': '2024-01-05T22:24:50.741+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:2'],\n",
      "            'type': 'prompt'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatPromptTemplate/final_output',\n",
      "  'value': {'messages': [SystemMessage(content='You are a helpful assistant.'),\n",
      "                         HumanMessage(content='what does eugene think of cats?')]}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatPromptTemplate/end_time',\n",
      "  'value': '2024-01-05T22:24:50.741+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '883e05a9-6b29-4494-92b3-51c72973cb54',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'ChatOpenAI',\n",
      "            'start_time': '2024-01-05T22:24:50.743+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:3'],\n",
      "            'type': 'llm'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': ''}})})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '{\\n'}})})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': ' '}})})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': ' \"'}})})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': 'query'}})})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '\":'}})})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': ' \"'}})})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': 'cats'}})})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '\"\\n'}})})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '}'}})})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/final_output',\n",
      "  'value': LLMResult(generations=[[ChatGeneration(generation_info={'finish_reason': 'function_call'}, message=AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}}))]], llm_output=None, run=None)},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/end_time',\n",
      "  'value': '2024-01-05T22:24:51.497+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/OpenAIFunctionsAgentOutputParser',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'f8052926-8eb1-4df1-a701-309ee48fd63c',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'OpenAIFunctionsAgentOutputParser',\n",
      "            'start_time': '2024-01-05T22:24:51.498+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:4'],\n",
      "            'type': 'parser'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/OpenAIFunctionsAgentOutputParser/final_output',\n",
      "  'value': AgentActionMessageLog(tool='get_eugene_thoughts', tool_input={'query': 'cats'}, log=\"\\nInvoking: `get_eugene_thoughts` with `{'query': 'cats'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})])},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/OpenAIFunctionsAgentOutputParser/end_time',\n",
      "  'value': '2024-01-05T22:24:51.499+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableSequence/final_output',\n",
      "  'value': {'output': AgentActionMessageLog(tool='get_eugene_thoughts', tool_input={'query': 'cats'}, log=\"\\nInvoking: `get_eugene_thoughts` with `{'query': 'cats'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})])}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/RunnableSequence/end_time',\n",
      "  'value': '2024-01-05T22:24:51.500+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': {'actions': [AgentActionMessageLog(tool='get_eugene_thoughts', tool_input={'query': 'cats'}, log=\"\\nInvoking: `get_eugene_thoughts` with `{'query': 'cats'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})])],\n",
      "            'messages': [AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})]}},\n",
      " {'op': 'replace',\n",
      "  'path': '/final_output',\n",
      "  'value': {'actions': [AgentActionMessageLog(tool='get_eugene_thoughts', tool_input={'query': 'cats'}, log=\"\\nInvoking: `get_eugene_thoughts` with `{'query': 'cats'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})])],\n",
      "            'messages': [AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})]}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/get_eugene_thoughts',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'e745d7ec-5992-4ab8-a3ef-191342c6470c',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'get_eugene_thoughts',\n",
      "            'start_time': '2024-01-05T22:24:51.501+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': [],\n",
      "            'type': 'tool'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/get_eugene_thoughts/final_output',\n",
      "  'value': {'output': \"[Document(page_content='cats like fish'), \"\n",
      "                      \"Document(page_content='dogs like sticks')]\"}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/get_eugene_thoughts/end_time',\n",
      "  'value': '2024-01-05T22:24:51.737+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': {'messages': [FunctionMessage(content=\"[Document(page_content='cats like fish'), Document(page_content='dogs like sticks')]\", name='get_eugene_thoughts')],\n",
      "            'steps': [{'action': AgentActionMessageLog(tool='get_eugene_thoughts', tool_input={'query': 'cats'}, log=\"\\nInvoking: `get_eugene_thoughts` with `{'query': 'cats'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})]),\n",
      "                       'observation': [Document(page_content='cats like fish'),\n",
      "                                       Document(page_content='dogs like sticks')]}]}},\n",
      " {'op': 'add',\n",
      "  'path': '/final_output/steps',\n",
      "  'value': [{'action': AgentActionMessageLog(tool='get_eugene_thoughts', tool_input={'query': 'cats'}, log=\"\\nInvoking: `get_eugene_thoughts` with `{'query': 'cats'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}})]),\n",
      "             'observation': [Document(page_content='cats like fish'),\n",
      "                             Document(page_content='dogs like sticks')]}]},\n",
      " {'op': 'add',\n",
      "  'path': '/final_output/messages/1',\n",
      "  'value': FunctionMessage(content=\"[Document(page_content='cats like fish'), Document(page_content='dogs like sticks')]\", name='get_eugene_thoughts')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableSequence:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '15d4ae13-554b-4144-9aa8-05527a15c042',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'RunnableSequence',\n",
      "            'start_time': '2024-01-05T22:24:51.739+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': [],\n",
      "            'type': 'chain'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableParallel<input,agent_scratchpad>:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '207dab98-d50c-4f2c-934d-291ba3ffd953',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'RunnableParallel<input,agent_scratchpad>',\n",
      "            'start_time': '2024-01-05T22:24:51.740+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:1'],\n",
      "            'type': 'chain'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>:3',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '8795a642-3f8e-4e5c-aec6-825b7aac2148',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': '<lambda>',\n",
      "            'start_time': '2024-01-05T22:24:51.741+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['map:key:input'],\n",
      "            'type': 'chain'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>:4',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '60be3aac-3718-4386-a941-1dd371b9a3ea',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': '<lambda>',\n",
      "            'start_time': '2024-01-05T22:24:51.741+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['map:key:agent_scratchpad'],\n",
      "            'type': 'chain'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>:3/final_output',\n",
      "  'value': {'output': 'what does eugene think of cats?'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/<lambda>:3/end_time',\n",
      "  'value': '2024-01-05T22:24:51.742+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>:4/final_output',\n",
      "  'value': {'output': [AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}}),\n",
      "                       FunctionMessage(content=\"[Document(page_content='cats like fish'), Document(page_content='dogs like sticks')]\", name='get_eugene_thoughts')]}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/<lambda>:4/end_time',\n",
      "  'value': '2024-01-05T22:24:51.742+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableParallel<input,agent_scratchpad>:2/final_output',\n",
      "  'value': {'agent_scratchpad': [AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}}),\n",
      "                                 FunctionMessage(content=\"[Document(page_content='cats like fish'), Document(page_content='dogs like sticks')]\", name='get_eugene_thoughts')],\n",
      "            'input': 'what does eugene think of cats?'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/RunnableParallel<input,agent_scratchpad>:2/end_time',\n",
      "  'value': '2024-01-05T22:24:51.742+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatPromptTemplate:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'f1ff89b0-ae9c-49d1-8a2e-73c7f001f1e7',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'ChatPromptTemplate',\n",
      "            'start_time': '2024-01-05T22:24:51.743+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:2'],\n",
      "            'type': 'prompt'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatPromptTemplate:2/final_output',\n",
      "  'value': {'messages': [SystemMessage(content='You are a helpful assistant.'),\n",
      "                         HumanMessage(content='what does eugene think of cats?'),\n",
      "                         AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_eugene_thoughts', 'arguments': '{\\n  \"query\": \"cats\"\\n}'}}),\n",
      "                         FunctionMessage(content=\"[Document(page_content='cats like fish'), Document(page_content='dogs like sticks')]\", name='get_eugene_thoughts')]}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatPromptTemplate:2/end_time',\n",
      "  'value': '2024-01-05T22:24:51.743+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'c4aeb3d0-33c3-4f46-80e9-d1ffb0f983f2',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'ChatOpenAI',\n",
      "            'start_time': '2024-01-05T22:24:51.744+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:3'],\n",
      "            'type': 'llm'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI:2/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': 'E'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='E')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': 'ug'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='ug')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': 'ene'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='ene')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' has'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' has')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' two'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' two')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' thoughts'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' thoughts')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' on'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' on')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' cats'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' cats')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '.'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='.')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' One'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' One')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' thought'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' thought')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' is'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' is')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' \"'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' \"')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': 'cats'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='cats')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' like'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' like')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' fish'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' fish')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '\"'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='\"')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' and'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' and')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' the'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' the')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' other'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' other')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' thought'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' thought')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' is'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' is')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' \"'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' \"')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': 'dogs'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='dogs')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' like'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' like')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' sticks'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' sticks')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '\".'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='\".')})\n",
      "--\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI:2/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='')})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/final_output',\n",
      "  'value': LLMResult(generations=[[ChatGeneration(text='Eugene has two thoughts on cats. One thought is \"cats like fish\" and the other thought is \"dogs like sticks\".', generation_info={'finish_reason': 'stop'}, message=AIMessage(content='Eugene has two thoughts on cats. One thought is \"cats like fish\" and the other thought is \"dogs like sticks\".'))]], llm_output=None, run=None)},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/end_time',\n",
      "  'value': '2024-01-05T22:24:53.082+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/OpenAIFunctionsAgentOutputParser:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '7074d8c2-df0b-4d18-9890-dd050c2f0ca9',\n",
      "            'metadata': {'__langserve_endpoint': 'stream_log',\n",
      "                         '__langserve_version': '0.0.37',\n",
      "                         '__useragent': 'python-httpx/0.25.2'},\n",
      "            'name': 'OpenAIFunctionsAgentOutputParser',\n",
      "            'start_time': '2024-01-05T22:24:53.083+00:00',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:4'],\n",
      "            'type': 'parser'}})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/OpenAIFunctionsAgentOutputParser:2/final_output',\n",
      "  'value': AgentFinish(return_values={'output': 'Eugene has two thoughts on cats. One thought is \"cats like fish\" and the other thought is \"dogs like sticks\".'}, log='Eugene has two thoughts on cats. One thought is \"cats like fish\" and the other thought is \"dogs like sticks\".')},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/OpenAIFunctionsAgentOutputParser:2/end_time',\n",
      "  'value': '2024-01-05T22:24:53.084+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableSequence:2/final_output',\n",
      "  'value': {'output': AgentFinish(return_values={'output': 'Eugene has two thoughts on cats. One thought is \"cats like fish\" and the other thought is \"dogs like sticks\".'}, log='Eugene has two thoughts on cats. One thought is \"cats like fish\" and the other thought is \"dogs like sticks\".')}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/RunnableSequence:2/end_time',\n",
      "  'value': '2024-01-05T22:24:53.084+00:00'})\n",
      "--\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': {'messages': [AIMessage(content='Eugene has two thoughts on cats. One thought is \"cats like fish\" and the other thought is \"dogs like sticks\".')],\n",
      "            'output': 'Eugene has two thoughts on cats. One thought is \"cats '\n",
      "                      'like fish\" and the other thought is \"dogs like '\n",
      "                      'sticks\".'}},\n",
      " {'op': 'add',\n",
      "  'path': '/final_output/output',\n",
      "  'value': 'Eugene has two thoughts on cats. One thought is \"cats like fish\" '\n",
      "           'and the other thought is \"dogs like sticks\".'},\n",
      " {'op': 'add',\n",
      "  'path': '/final_output/messages/2',\n",
      "  'value': AIMessage(content='Eugene has two thoughts on cats. One thought is \"cats like fish\" and the other thought is \"dogs like sticks\".')})\n"
     ]
    }
   ],
   "source": [
    "async for chunk in remote_runnable.astream_log({\"input\": \"what does eugene think of cats?\"}):\n",
    "    print('--')\n",
    "    print(chunk)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
